package com.hzpd.hainan.module.usercenter.acticity;


import android.os.Bundle;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.hzpd.hainan.R;
import com.hzpd.hainan.app.ToolBarActivity;
import com.hzpd.hainan.config.CODE;
import com.hzpd.hainan.model.usercenter.EmptyEntity;

import com.hzpd.hainan.net.Factory;
import com.hzpd.hainan.utils.LogUtils;
import com.hzpd.hainan.utils.ParamUtils;
import com.hzpd.hainan.utils.TUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class UserSuggestActivity extends ToolBarActivity {
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.email_user_suggest)
    EditText email_user_suggest;
    @BindView(R.id.content_user_suggest)
    EditText content_user_suggest;
    @BindView(R.id.btn_commit_suggest)
    Button btn_commit_suggest;

    @Override
    public String initToolBarTitle() {
        return null;
    }

    @Override
    public int setMyContentView() {
        return R.layout.activity_user_suggest;
    }

    @Override
    public void initView() {
        if (spu.getLanguage().equals(CODE.ZHONGWEN)) {
            tv_title.setText("建议反馈");
            email_user_suggest.setHint("邮箱");
            content_user_suggest.setHint("请输入您的建议（200字以内）");
            btn_commit_suggest.setText("提交");
        }else{
            tv_title.setText("བསམ་ཚུལ་འདོན་པ།");
            email_user_suggest.setHint("ཡིག་སྒམ།");
            content_user_suggest.setHint("བསམ་ཚུལ་བརྗོད་པ། ཡིག་འབྲུ་ཉིས་བརྒྱའི་ཚད།");
            btn_commit_suggest.setText("ཡར་སྤྲོད།");
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }



    @OnClick(R.id.btn_commit_suggest)
    public void commit(View view) {
//        String s_title=title_user_suggest.getText().toString().trim();
        String s_email = email_user_suggest.getText().toString().trim();
        String s_content = content_user_suggest.getText().toString().trim();
//        if (s_title==null||"".equals(s_title)){
//            TUtils.toast("请输入标题");
//            return;
//        }
        if (s_email == null || "".equals(s_email)) {
            TUtils.toast("请输入邮箱");
            return;
        }
        if (s_content == null || "".equals(s_content)) {
            TUtils.toast("请输入内容");
            return;
        }
        showDialog();
        suggest(s_email, s_content);

    }

    private void suggest(String email, String content) {
        Map<String, String> map = new HashMap<>();
        map.put("siteid", "1");
        map.put("Email", email);
        map.put("content", content);
        Factory.provideHttpService().userSuggest(ParamUtils.getRequestParam(map))
                .onBackpressureBuffer()

                .subscribeOn(Schedulers.io())

                .filter(new Func1<EmptyEntity, Boolean>() {
                    @Override
                    public Boolean call(EmptyEntity emptyEntity) {
                        LogUtils.e( "call: "+emptyEntity.code);

                        return true;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<EmptyEntity>() {
                    @Override
                    public void call(EmptyEntity emptyEntity) {
                        disMissDialog();
                        TUtils.toast(emptyEntity.msg);

                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        LogUtils.e( "call: "+throwable.getMessage());

                    }
                });
    }
}
